package com.project.isaacwiki.service.impl;

import com.project.isaacwiki.dto.UserRegister;
import com.project.isaacwiki.mapper.MapperLogin;
import com.project.isaacwiki.mapper.MapperUserOpera;
import com.project.isaacwiki.pojo.ImgPath;
import com.project.isaacwiki.pojo.Users;
import com.project.isaacwiki.service.ServiceUserOpera;
import com.project.isaacwiki.util.AliyunOSSOperator;
import com.project.isaacwiki.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ServiceUserOperaImpl implements ServiceUserOpera {

    @Autowired
    private MapperUserOpera mapperUserOpera;

    @Autowired
    private MapperLogin mapperLogin;

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Override
    public Result updateImage(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return Result.Success("no update");
        }
        return Result.Success(aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename(), "image"));
    }

    @Override
    public Result getUser(Integer id) {
        Users user = mapperUserOpera.queryUserById(id);
        ImgPath imgPath = mapperUserOpera.queryImage(id);
        if (user == null) {
            return Result.Error("用户不存在");
        }
        if (user.getState() == 0) {
            return Result.Error("用户被禁用");
        }
        user.setPassword(null);
        UserRegister userRegister = new UserRegister(user, imgPath.getPath());
        return Result.Success(userRegister);
    }

    @Override
    @Transactional
    public Result updateUserInfo(Integer id, String name, String imgUrl) throws Exception {
        if (id == null) {
            return Result.Error("id不存在！");
        }

        if (name != null && !name.isEmpty()) {
            if (mapperUserOpera.updateUserInfo(name, id) == 0) {
                throw new Exception("更新失败");
            }
        }

        if (imgUrl != null && !imgUrl.equals("no update") && !imgUrl.isEmpty()) {
            ImgPath imgPathOld = mapperUserOpera.queryImage(id);
            if (mapperUserOpera.updateImage(imgUrl, id) == 0) {
                throw new Exception("更新失败");
            }
            if (imgPathOld != null) {
                String fileName = imgPathOld.getPath().substring(imgPathOld.getPath().lastIndexOf("/") + 1);
                aliyunOSSOperator.delete(fileName, "image");
            }
        }

        Users user = mapperUserOpera.queryUserById(id);
        user.setPassword(null);
        return Result.Success(user);
    }
    @Override
    @Transactional
    public Result updateUserPasswordById(Integer id, String oldPassword, String newPassword) throws Exception {
        Integer user = mapperUserOpera.updateUserPasswordById(id, oldPassword, newPassword);
        if (user == 0) {
            return Result.Error("密码错误");
        }
        return Result.Success(user);
    }
}
